﻿#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <locale.h>
int main()
{
	char* a = setlocale(LC_ALL, "");
	wchar_t wc1 = L'·';
	wchar_t wc2 = L'叶';
	wchar_t wc3 = L'天';
	wchar_t wc4 = L'帝';
	wchar_t wc5 = L'·';
	wprintf(L"%lc", wc1);
	wprintf(L"%lc", wc2);
	wprintf(L"%lc", wc3);
	wprintf(L"%lc", wc4);
	wprintf(L"%lc", wc5);
	return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include "List.h"
L* LInit()//初始化//创建哨兵位
{
	L* list = (L*)malloc(sizeof(L));
	if (list == NULL)
	{
		perror("malloc");
		exit(1);
	}
	list->date = -1;
	list->next = list;
	list->prev = list;
	return list;
}
void LPrint(L* plist)
{
	assert(plist);
	L* pcur = plist->next;
	while (pcur != plist)
	{
		printf("%d->", pcur->date);
		pcur = pcur->next;
	}
	printf("\n");
}
L* LCreat(Listtype x)
{
	L* newnode = (L*)malloc(sizeof(L));
	if (newnode == NULL)
	{
		perror("malloc");
		exit(1);
	}
	newnode->date = x;
	newnode->next = newnode->prev = newnode;
	return newnode;
}
void LPushFront(L* plist, Listtype x)//头插
{
	assert(plist);
	L* newnode = LCreat(x);
	newnode->next = plist->next;
	newnode->prev = plist;
	plist->next->prev = newnode;
	plist->next = newnode;
}
void LPushBack(L* plist, Listtype x)//尾插
{
	assert(plist);
	L* newnode = LCreat(x);
	newnode->prev = plist->prev;
	newnode->next = plist;
	plist->prev->next = newnode;
	plist->prev = newnode;
}
void LDelFront(L* plist)//头删
{
	assert(plist);
	L* del = plist->next;
	plist->next = del->next;
	del->next->prev = plist;//del->prev = plist 我犯的错误
	free(del);
	del = NULL;
}
void LDelBack(L* plist)//尾删
{
	//assert(plist);//判断不完全
	assert(plist && plist->next != plist);
	L* del = plist->prev;
	plist->prev = del->prev;
	plist->prev->next = plist;
	free(del);
	del = NULL;
}
L* LFind(L* plist, Listtype x)//查找
{
	assert(plist);
	L* pcur = plist->next;//犯错了，L* pcur = plist;不是从第一个有效节点开始
	while (pcur != plist)
	{
		if (pcur->date == x)
		{
			return pcur;
		}
		pcur = pcur->next;
	}
	return NULL;
}
void LInsert(L* pos, Listtype x)//在pos之后增加
{
	assert(pos);
	L* newnode = LCreat(x);
	newnode->next = pos->next;
	newnode->prev = pos;
	pos->next->prev = newnode;
	pos->next = newnode;
}
void LErase(L* pos)//删除pos节点
{
	assert(pos && pos != pos->next);
	pos->prev->next = pos->next;
	pos->next->prev = pos->prev;
	free(pos);
	pos = NULL;//实际上pos走除循环后没有被置空指针，传递的一级指针形参不会对实参该变。
}
void LDestroy(L* plist)//销毁
{
	assert(plist);
	L* del = plist->next;
	L* pcur = plist->next;
	while (del != plist)
	{
		pcur = pcur->next;
		free(del);
		del = pcur;
	}
	free(plist);
	plist = NULL;
}